//
//  SCVinChecker.c
//  OCRDemo
//
//  Created by 党帅 on 2019/6/26.
//  Copyright © 2019 党帅. All rights reserved.
//

#include "SCVinChecker.h"


static char codeValue[26] = {
    1,2,3,4,5,6,7,8,0,1,2,3,4,5,0,7,8,9,2,3,4,5,6,7,8,9
};

bool checkVinCode(const char * _Nullable vin) {
    long total = 0;
    char checkDigit = 0;
    int digit_value = 8;
    for (int i = 0; i < 17; i++) {
        char cur = vin[i];
        if (i == 8) {
            checkDigit = cur;
            continue;
        }
        if( cur >= '0' && cur <= '9') {
            int s = (cur - '0') * digit_value;
            printf("%d, %d\n", cur-'0', digit_value);
            total = total + s;
        } else if (cur >= 'A' && cur <= 'Z') {
            int code_value = codeValue[cur-65];
            printf("%d, %d\n", code_value, digit_value);
            total = total + code_value * digit_value;
        }
        digit_value--;
        if (digit_value == 1) {
            digit_value = 10;
        }
    }
    
    int check = total % 11;
    if (check == 10) {
        return 'X' == checkDigit;
    }
    printf("%s == %ld mol --> %d \n",vin,total,check);
    return (check + '0') == checkDigit;
}
